﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using MyPoints.Model;

namespace MyPoints
{
  public static class Shared
  {
    public const string NEW_WPT_NAME = "<< unnamed >>";
    public static eSettingsType sett = null;
    public static GpxModel model = new GpxModel();

    private class ByteArrayImage
    {
      private byte[] data;
      private int width;
      private int height;
      private int stride;

      public ByteArrayImage(byte[] data, int width, int height, int stride)
      {
        this.data = data;
        this.width = width;
        this.height = height;
        this.stride = stride;
      }

      public Color GetPixel(int x, int y)
      {
        int ir, ig, ib, ia;
        GetArrayIndices(x, y, out ir, out ig, out ib, out ia);

        byte r, g, b, a;
        r = data[ir];
        g = data[ig];
        b = data[ib];
        a = data[ia];

        Color c = Color.FromArgb(a, r, g, b);

        return c;
      }

      private void GetArrayIndices(int x, int y, out int ir, out int ig, out int ib, out int ia)
      {
        ib = y * this.width * 4 + x * 4; //x * this.pixelWidth + this.stride * y;
        ig = ib + 1;
        ir = ig + 1;
        ia = ir + 1;
      }

      public void SetPixel(int x, int y, Color c)
      {
        int ir, ig, ib, ia;
        GetArrayIndices(x, y, out ir, out ig, out ib, out ia);

        this.data[ir] = c.R;
        this.data[ig] = c.G;
        this.data[ib] = c.B;
        this.data[ia] = c.A;
      }

      public int Width { get { return this.width; } }
      public int Height { get { return this.height; } }
    }

    public static string ImageToString(System.Drawing.Image image)
    {
      string ret;
      MemoryStream ms = new MemoryStream();

      image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
      byte[] bytes = ms.ToArray();

      ret = Convert.ToBase64String(bytes);

      return ret;
    }

    public static BitmapImage StringToBitmapImage(string imgString)
    {
      BitmapImage ret;

      byte[] bytes = Convert.FromBase64String(imgString);
      MemoryStream ms = new MemoryStream(bytes);

      ret = new BitmapImage();
      ret.BeginInit();
      ret.StreamSource = ms;
      ret.EndInit();

      return ret;
    }

    internal static BitmapSource UpdateBitmap(BitmapImage bmp, Color fillColor)
    {
      WriteableBitmap wbm = new WriteableBitmap(bmp);
      int stride = (wbm.PixelWidth * wbm.Format.BitsPerPixel + 7) / 8; // wbm.PixelWidth * wbm.Format.BitsPerPixel / 8;
      byte[] data = new byte[(int)(wbm.PixelWidth * stride)];
      int offset = 0;
      wbm.CopyPixels(data, stride, offset);

      ByteArrayImage bai = new ByteArrayImage(data, wbm.PixelWidth, wbm.PixelHeight, stride);

      Color replaceColor = Color.FromArgb(255, 255, 255, 255);

      for (int y = 0; y < bai.Height; y++)
      {
        for (int x = 0; x < bai.Width; x++)
        {
          Color c = bai.GetPixel(x, y);
          if (c.R == replaceColor.R && c.G == replaceColor.G && c.B == replaceColor.B && c.A == replaceColor.A)
          {
            bai.SetPixel(x, y, fillColor);
          }
        }
      }

      WriteableBitmap ret = new WriteableBitmap(wbm.PixelWidth, wbm.PixelHeight, wbm.DpiX, wbm.DpiY, wbm.Format, wbm.Palette);
      ret.WritePixels(new System.Windows.Int32Rect(0, 0, wbm.PixelWidth, wbm.PixelHeight), data, stride, 0);

      return ret;
    }

    internal static Color GetMarkerFillColorForWptType(wptType wpt)
    {
      return GetMarkerFillColorForWptType(wpt.Category);
    }
    internal static Color GetMarkerFillColorForWptType(string category)
    {
      int seed;
      if (category != null)
        seed = category.GetHashCode();
      else
        seed = 0;
      Random rnd = new Random(seed);
      byte r = (byte)rnd.Next(256);
      byte g = (byte)rnd.Next(256);
      byte b = (byte)rnd.Next(256);
      byte a = (byte)255;
      Color ret = Color.FromArgb(a, r, g, b);
      return ret;
    }
  }
}
